//index.js
//获取应用实例
const app = getApp();
const util = require('../../utils/util');
const common = require('../../utils/common');
const requestUtil = require('../../utils/request');
const api = require('../../config/api');

Page({
  data: {
    CustomBar: app.globalData.CustomBar,
    hasCardInfo: false, // 是否有卡片信息
    cardInfo: {}, // 卡片信息
    visitedNum: 0, // 访问量
    todayVisitedNum: 0, // 今日访问量
    reInitToGetPhone: true, // 重置获取手机号
  },

  onShow: function () {
    let userType = app.globalData.userType;
    this.setData({
      hasCardInfo: app.globalData.hasCardInfo,
      hasGetPhoneNumber: app.globalData.hasGetPhoneNumber,
      userType: userType
    });
    if (userType == 1) {
      this.setData({
        cardInfo: app.globalData.employeeInfo
      })
      this.getCompanyInfo(app.globalData.employeeInfo.companyId)
    } else {
      this.getCompanyInfo(app.globalData.companyId);
    }
  },

  getCompanyInfo: async function (id) {
    if(!id) {
        this.getCompanyList();
        return;
    }
    let res = await requestUtil.request("POST", api.urls.getCompanyDetail, {
      id: id ? id : 1
    });
    if (res.data.code != 0) {
      util.showToast(res.data.msg);
      return;
    };
    let data = res.data.data;
    data.companyInfo = common.formatRichText(common.formatImg(data.companyInfo));
    this.setData({
      companyInfo: data
    })
  },

    /**
   * 获取公司列表
   */
  getCompanyList: async function () {
    let res = await requestUtil.request("POST", api.urls.getCompanyList, null);
    if (res.data.code != 0) {
      util.showToast(res.data.msg);
      return;
    }
    let data = res.data.data;
    for(let i in data) {
        if(data[i].appid == api.appid) {
            let va = data[i];
            va.companyInfo = common.formatRichText(common.formatImg(va.companyInfo));
            this.setData({
                companyInfo: va
            })
            break;
        }
    }
    
  },

  /**
   * 复制到剪切板
   */
  copyData: function (o) {
    let val = o.currentTarget.dataset.val
    wx.setClipboardData({
      data: val
    })
  },

  /**
   * 拨打电话 
   */
  callup: function () {
    let phone = this.data.cardInfo.mobile;
    if (this.data.userType == 1) {
      phone = this.data.cardInfo.mobile;
    } else {
      phone = this.data.companyInfo.telphone1;
    }
    if (phone == null) {
      wx.showToast({
        title: '对象未登记手机号',
        icon: 'none'
      })
      return;
    } else {
      wx.makePhoneCall({
        phoneNumber: phone
      })
    }
  },

  /**
   * 导航
   */
  navigateTo: function (o) {
    let address = this.data.companyInfo.companyAddr;
    let latitude = Number(this.data.companyInfo.latitude); // 纬度
    let longitude = Number(this.data.companyInfo.longitude); // 经度
    let name = this.data.companyInfo.companyName;
    if (address == null) {
      wx.showToast({
        title: '对象未登记地址',
        icon: 'none'
      })
      return;
    } else {
      wx.openLocation({
        latitude: latitude,
        longitude: longitude,
        name: name,
        address: address
      })
    }
  },
})